package win.bigwhale.sudoku.view;

import java.util.Optional;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import win.bigwhale.sudoku.SudokuUtil;
import win.bigwhale.sudoku.controller.GameControlInterface;
import win.bigwhale.sudoku.controller.GameController;
import win.bigwhale.sudoku.game.Sudoku;

public class ControlButtonPane extends VBox implements GameControlInterface {
	private static ControlButtonPane controlButtonPane = new ControlButtonPane();
	private Button pauseButton = new Button(SudokuUtil.PAUSE_GAME_BUTTON);
	private Button restartButton = new Button(SudokuUtil.RESTART_GAME_BUTTON);
	private Button endButton = new Button(SudokuUtil.END_GAME_BUTTON);
	private Button newGameButton = new Button(SudokuUtil.NEW_GAME_BUTTON);

	private ControlButtonPane() {
		GameController.getGameController().addPane(this);
		setPrefSize(130.0, 190.0);
		setLayoutX(10.0);
		setLayoutY(130.0);
		setBackground(SudokuUtil.PANE_BACKGROUND);
		setAlignment(Pos.CENTER);
		setPadding(new Insets(15.0, 0.0, 0.0, 0.0));

		initButton(pauseButton, restartButton, endButton, newGameButton);

		pauseButton.setOnMouseClicked(new PauseAndResumeEventHandler());
		restartButton.setOnMouseClicked(new RestartEventHandler());
		endButton.setOnMouseClicked(new EndGameEventHandler());
		newGameButton.setOnMouseClicked(new StartNewGameFromPlayFieldEventHandler());
	}

	public void initButton(Button... buttons) {
		Insets buttonInsets = new Insets(0.0, 0.0, 10.0, 0.0);
		for (int i = 0; i < buttons.length; i++) {
			VBox.setMargin(buttons[i], buttonInsets);
			buttons[i].setPrefSize(SudokuUtil.CONTROL_PANE_BUTTON_WIDTH, SudokuUtil.CONTROL_PANE_BUTTON_HEIGHT);
		}
		getChildren().addAll(buttons);
	}

	public static ControlButtonPane getControlButtonPane() {
		return controlButtonPane;
	}


	private final class StartNewGameFromPlayFieldEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			GameController.getGameController().pauseGame();
			ChooseGame.getChooseGame().show();
			setAllButtonDisable (true);
		}
	}

	private final class EndGameEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			Optional<ButtonType> rs = showAndWait("Are you sure to end this game?",
					"Click ok to give up game, or click cancel to continue.",
					"Confirm");
			if (rs.get().getButtonData().equals(ButtonData.OK_DONE)) {
				GameController.getGameController().endGame();
			}
		}
	}

	private final class RestartEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			Optional<ButtonType> rs = showAndWait("Are you sure to restart this game?",
					"Click ok to restart this game, or click cancel to continue.",
					"Confirm");
			if (rs.get().getButtonData().equals(ButtonData.OK_DONE)) {
				GameController.getGameController().restartGame();
			}
		}
	}

	private Optional<ButtonType> showAndWait(String headerText, String contentText, String title) {
		Alert confirmAlert = new Alert(AlertType.CONFIRMATION, 
				null,
				new ButtonType("OK", ButtonData.OK_DONE), 
				new ButtonType("Cancel", ButtonData.CANCEL_CLOSE));
		confirmAlert.setTitle(title);
		confirmAlert.setHeaderText(headerText);
		confirmAlert.setContentText(contentText);
		Optional<ButtonType> rs = confirmAlert.showAndWait();
		return rs;
	}

	private final class PauseAndResumeEventHandler implements EventHandler<MouseEvent> {
		@Override
		public void handle(MouseEvent event) {
			if (((Button)event.getSource()).getText().compareTo(SudokuUtil.PAUSE_GAME_BUTTON) == 0) {
				GameController.getGameController().pauseGame();
			} else {
				GameController.getGameController().resumeGame();
			}
			
		}
	}

	public void setAllButtonDisable (boolean disable) {
		pauseButton.setDisable(disable);
		restartButton.setDisable(disable);
		endButton.setDisable(disable);
		newGameButton.setDisable(disable);
	}

	@Override
	public void startGame(Sudoku sudoku) {}

	@Override
	public void pauseGame() {
		pauseButton.setText(SudokuUtil.RESUME_GAME_BUTTON);
	}

	@Override
	public void restartGame() {
		pauseButton.setText(SudokuUtil.PAUSE_GAME_BUTTON);
	}

	@Override
	public void endGame() {
		pauseButton.setText(SudokuUtil.PAUSE_GAME_BUTTON);
	}

	@Override
	public void completeGame() {
		pauseButton.setDisable(true);
		endButton.setDisable(true);
	}

	@Override
	public void resumeGame() {
		pauseButton.setText(SudokuUtil.PAUSE_GAME_BUTTON);
	}
}
